﻿
using RTW.DAL;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.Meeting.Servers
{
    public class MeetingRoomServers
    {
        private readonly IRepository<meeting_room> meeting_roomRepository;

        public MeetingRoomServers()
        {
            meeting_roomRepository = new DapperRepository<meeting_room>();

        }

        public List<meeting_room> GetData(string MR_NAME, string MR_CAPACITY, string VIDEO_TYPE)
        {
            var sql = "select * from meeting_room where 1=1";
            if (MR_NAME != null)
                sql += " and MR_NAME like @MR_NAME";
            if (MR_CAPACITY != null)
                sql += " and MR_CAPACITY=@MR_CAPACITY";
            if (VIDEO_TYPE != null)
                sql += " and VIDEO_TYPE=@VIDEO_TYPE";

            return meeting_roomRepository.SqlWithParams(sql, new { MR_NAME = "%" + MR_NAME + "%", MR_CAPACITY, VIDEO_TYPE }).ToList();
        }

        public meeting_room GetModelById(string id)
        {
            return meeting_roomRepository.GetById(id);
        }


        internal void Add(meeting_room model)
        {
            meeting_roomRepository.Insert(model);
        }

        internal void Update(meeting_room model)
        {
            meeting_roomRepository.Update(model);
        }

        internal void Del(string id)
        {
            meeting_roomRepository.DeleteByIds(id);
        }


        internal List<SelectListItem> GetTO_ID()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = meeting_roomRepository.SqlWithParamsDynamic("select DEPT_ID Value,DEPT_NAME Text from [dbo].[department]");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }

        public string GetTO_IDName(string id)
        {
            string DEPT_NAME = string.Empty;
            var DEPT = meeting_roomRepository.SqlWithParamsDynamic("select DEPT_NAME from [dbo].[department] where DEPT_ID = @DEPT_ID", new { DEPT_ID = id });
            foreach (var name in DEPT)
            {
                DEPT_NAME = name.DEPT_NAME.ToString().Trim();
            }
            return DEPT_NAME;
        }

        internal List<SelectListItem> GetSECRET_TO_ID()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = meeting_roomRepository.SqlWithParamsDynamic("select [USER_ID] as Value,[USER_NAME]+'-----'+[TRUE_NAME] as Text from [dbo].[users]");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }
        public string GetSECRET_TO_IDName(string id)
        {
            string users_NAME = string.Empty;
            var users = meeting_roomRepository.SqlWithParamsDynamic("select USER_NAME  from [dbo].[users] where USER_ID=@USER_ID", new { USER_ID = id });
            foreach (var name in users)
            {
                users_NAME = name.USER_NAME.ToString().Trim();
            }
            return users_NAME;
        }


        internal List<SelectListItem> GetOPERATOR()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = meeting_roomRepository.SqlWithParamsDynamic("select a.name Value,concat(a.name,'-----',convert(nvarchar(max),b.value)) Text from sys.tables  a inner join  sys.extended_properties b on a.object_id=b.major_id and b.minor_id=0");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }


    }
}